
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="快递公司:" prop="company_name">
            <el-input v-model="ruleForm.company_name" placeholder="请输入快递公司名称" />
          </el-form-item>
          <el-form-item label="快递代码:" prop="company_code">
            <el-input v-model="ruleForm.company_code" placeholder="请输入快递代码" />
          </el-form-item>
          <el-form-item label="网点名称:" prop="branch_name">
            <el-input v-model="ruleForm.branch_name" placeholder="请输入网点名称" />
          </el-form-item>
          <el-form-item label="网点代码:" prop="branch_code">
            <el-input v-model="ruleForm.branch_code" placeholder="请输入快递代码" />
          </el-form-item>

          <el-form-item label="联系人:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入联系人" />
          </el-form-item>
          <el-form-item label="区号:" prop="area_code">
            <el-select
              v-model="ruleForm.area_code"
              style="width:100%"
              placeholder="请选择区号"
              clearable
            >
              <el-option
                v-for="(item,index) in codeList"
                :key="index"
                :label="'+' + item.area_code + item.area_name"
                :value="item.area_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="手机号:" prop="mobile">
            <el-input v-model="ruleForm.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="选择区域" prop="nation">
            <el-cascader
              v-model="areaList"
              filterable
              :props="codes"
              placeholder="请选择区域"
              :options="linkList"
              style="width: 100%"
              @change="changeArea()"
            />
          </el-form-item>
          <el-form-item label="详细地址:" prop="detailed">
            <el-input v-model="ruleForm.detailed" type="textarea" placeholder="请输入详细地址" />
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="app_key:" prop="app_key">
            <el-input v-model="ruleForm.app_key" placeholder="请输入app_key" />
          </el-form-item>
          <el-form-item label="app_secret:" prop="app_secret">
            <el-input v-model="ruleForm.app_secret" placeholder="请输入app_secret" />
          </el-form-item>
          <el-form-item label="其他参数:" prop="arguments">
            <el-input v-model="ruleForm.arguments" type="textarea" placeholder="请输入其他参数" />
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          v-if="status == 1"
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createCourier, updateCourier } from '@/api/base/express'
import { listMobileAreaCode } from '@/api/user'
import { countryAddressInfo } from '@/api/base/country'
import { mapState } from 'vuex'
export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },

  data() {
    return {
      showDialog: false,
      codes: {
        label: 'name',
        value: 'name',
        children: '_child'
      },
      ruleForm: {
        'company_name': '',

        'company_code': '',

        'branch_name': '',

        'branch_code': '',

        'name': '',

        'area_code': '',

        'mobile': '',

        'status': '',

        'app_key': '',

        'app_secret': '',

        'arguments': '',

        'nation': '',
        'province': '',
        'city': '',
        'district': '',
        'detailed': ''
      },
      rules: {
        company_name: [
          { required: true, message: '请输入快递公司名称', trigger: 'blur' }
        ],
        company_code: [
          { required: true, message: '请输入快递代码' }
        ],
        branch_name: [
          { required: true, message: '请输入网点名称' }
        ],
        branch_code: [
          { required: true, message: '请输入快递代码' }
        ],

        name: [
          { required: true, message: '请输入联系人' }
        ],
        area_code: [
          { required: true, message: '请选择区号' }
        ],
        mobile: [
          { required: true, message: '请输入手机号' }
        ],
        nation: [
          { required: true, message: '请选择区域' }
        ],
        detailed: [
          { required: true, message: '请输入详细地址' }
        ],
        status: [
          { required: true, message: '请选择状态' }
        ]
      },
      areaList: [],
      codeList: []
    }
  },
  computed: {
    linkList: {
      get() { return this.$store.state.app.linkList },
      set(value) { }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        this.areaList = []
        if (this.status === 2) {
          this.ruleForm = this.detial
          this.areaList.push(this.ruleForm.nation, this.ruleForm.province, this.ruleForm.city, this.ruleForm.district)
        }
      }
    }
  },
  mounted() {
    this.getListMobileAreaCode()
    this.getCountryAddressInfo()
  },
  methods: {
    changeArea() {
      this.areaList.push('')
      this.ruleForm.nation = this.areaList[0]
      this.ruleForm.province = this.areaList[1]
      this.ruleForm.city = this.areaList[2]
      this.ruleForm.district = this.areaList[3]
    },
    getListMobileAreaCode() {
      listMobileAreaCode().then(res => {
        this.codeList = res.data.list
      })
    },
    getCountryAddressInfo() {
      if (!this.linkList.length) {
        countryAddressInfo().then(res => {
          this.linkList = res.data
          this.$store.dispatch('app/setLink', res.data)
        })
      }
    },
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.ruleForm = {
        'company_name': '',

        'company_code': '',

        'branch_name': '',

        'branch_code': '',

        'name': '',

        'area_code': '',

        'mobile': '',

        'status': '',

        'app_key': '',

        'app_secret': '',

        'arguments': '',

        'nation': '',
        'province': '',
        'city': '',
        'district': '',
        'detailed': ''
      }
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createCourier(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateCourier({
            'courier_id': this.ruleForm.id,
            'company_name': this.ruleForm.company_name,
            'company_code': this.ruleForm.company_code,
            'branch_name': this.ruleForm.branch_name,
            'branch_code': this.ruleForm.branch_code,
            'name': this.ruleForm.name,
            'area_code': this.ruleForm.area_code,
            'mobile': this.ruleForm.mobile,

            'nation': this.ruleForm.nation,
            'province': this.ruleForm.province,
            'city': this.ruleForm.city,
            'district': this.ruleForm.district,
            'detailed': this.ruleForm.detailed,

            'status': this.ruleForm.status,
            'app_key': this.ruleForm.app_key,
            'app_secret': this.ruleForm.app_secret,
            'arguments': this.ruleForm.arguments

          }).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
